package org.dtrd.modules.patient.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.common.system.vo.LoginUser;
import org.dtrd.modules.patient.entity.bean.group.GroupDetail;
import org.dtrd.modules.patient.entity.bean.group.GroupListInfo;
import org.dtrd.modules.patient.entity.po.DtrdEntRdGroup;
import org.dtrd.modules.patient.mapper.DtrdEntRdGroupMapper;
import org.dtrd.modules.patient.service.IDtrdEntRdGroupService;
import org.dtrd.modules.patient.service.IDtrdRlGroupPatientService;
import org.dtrd.modules.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
@author 萧
@date 2021-10-01 22:47:27
**/
@Service
@DS("multi-datasource1")
public class DtrdEntRdGroupServiceImpl extends ServiceImpl<DtrdEntRdGroupMapper, DtrdEntRdGroup> implements IDtrdEntRdGroupService {

    @Autowired
    private IDtrdRlGroupPatientService groupPatientRlService;

    @Override
    public List<GroupListInfo> getGroupList() {
        LambdaQueryWrapper<DtrdEntRdGroup> queryWrapper = baseQueryWrapper();
        List<DtrdEntRdGroup> poList = list(queryWrapper);
        List<GroupListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(GroupListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public GroupDetail getGroup(Integer dataId) {
        DtrdEntRdGroup dtrdEntRdGroup = getById(dataId);
        return Optional.ofNullable(dtrdEntRdGroup)
                .map(po -> new GroupDetail().parseFromPO(po))
                .orElse(null);
    }

    @Override
    public boolean isValidGroupName(String groupName, Integer groupId, String orgCode) {
        LambdaQueryWrapper<DtrdEntRdGroup> wrapper = baseQueryWrapper()
                .eq(DtrdEntRdGroup::getGroupName, groupName)
                .eq(DtrdEntRdGroup::getOrgCode, orgCode)
                .last("limit 1");
        DtrdEntRdGroup group = getOne(wrapper);
        return group == null || group.getDataId().equals(groupId);
    }

    @Override
    public boolean saveOrUpdateGroup(GroupDetail dto) {
        DtrdEntRdGroup dtrdEntRdGroup = new DtrdEntRdGroup();
        if(dto.getDataId() == null){
            dtrdEntRdGroup = dtrdEntRdGroup.init();
        }
        dtrdEntRdGroup.parseFromDTO(dto);
        LoginUser loginUser = ShiroUtil.getLoginUser();
        if (loginUser != null) {
            loginUser.setOrgCode(loginUser.getOrgCode());
        }
        return saveOrUpdate(dtrdEntRdGroup);
    }

    @Override
    public boolean removeGroup(Integer dataId) {
        // 删除分组下的所有关系记录
        groupPatientRlService.removeGroupPatients(dataId);
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntRdGroup> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdGroup.class)
                .eq(DtrdEntRdGroup::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntRdGroup> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntRdGroup.class)
                .eq(DtrdEntRdGroup::getIsDel, 0);
    }
}
